package com.xzx.study;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

public class DialogActivity extends AppCompatActivity implements View.OnClickListener {

    private Button alert_dialog1;
    private Button alert_dialog2;
    private Button alert_dialog3;
    private Button progress_dialog1;
    private Button progress_dialog2;
    private Button date_picker_dialog;
    private Button time_picker_dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);

        alert_dialog1 = findViewById(R.id.alert_dialog1);
        alert_dialog2 = findViewById(R.id.alert_dialog2);
        alert_dialog3 = findViewById(R.id.alert_dialog3);
        progress_dialog1 = findViewById(R.id.progress_dialog1);
        progress_dialog2 = findViewById(R.id.progress_dialog2);
        date_picker_dialog = findViewById(R.id.date_picker_dialog);
        time_picker_dialog = findViewById(R.id.time_picker_dialog);

        alert_dialog1.setOnClickListener(this);
        alert_dialog2.setOnClickListener(this);
        alert_dialog3.setOnClickListener(this);
        progress_dialog1.setOnClickListener(this);
        progress_dialog2.setOnClickListener(this);
        date_picker_dialog.setOnClickListener(this);
        time_picker_dialog.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == alert_dialog1) {
            showAlertDialog();
        } else if (v == alert_dialog2) {
            showAlertDialog2();
        } else if (v == alert_dialog3) {
            showAlertDialog3();
        } else if (v == progress_dialog1) {
            showProgressDialog1();
        } else if (v == progress_dialog2) {
            showProgressDialog2();
        } else if (v == date_picker_dialog) {
            showDatePickerDialog();
        } else if (v == time_picker_dialog) {
            showTimePickerDialog();
        }
    }

    private void showTimePickerDialog() {
        Calendar c = Calendar.getInstance();
        int hourOfDay = c.get(Calendar.HOUR_OF_DAY); //得到小时
        int minute = c.get(Calendar.MINUTE); //得到分钟

        TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Toast.makeText(DialogActivity.this, hourOfDay + ":" + minute, 0).show();
            }
        }, hourOfDay, minute, true);
        timePickerDialog.show();
    }

    private void showDatePickerDialog() {
        //创建日历对象
        Calendar calendar = Calendar.getInstance();
        //得到当前的年月日
        final int year = calendar.get(Calendar.YEAR);//得到年份
        final int monthOfYear = calendar.get(Calendar.MONTH);//月
        final int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);//得到日

        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Toast.makeText(DialogActivity.this, year + ":" + month + ":" + dayOfMonth, 0).show();
            }
        }, year, monthOfYear, dayOfMonth);
        datePickerDialog.show();
    }

    private void showProgressDialog2() {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("Title");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMax(50);
        progressDialog.setProgress(10);
        progressDialog.show();
        new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 50; i++) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                    }
                    progressDialog.setProgress(progressDialog.getProgress() + 1);
                }
                progressDialog.dismiss();
            }
        }.start();
    }

    private void showProgressDialog1() {
        final ProgressDialog progressDialog = ProgressDialog.show(this, "数据加载", "数据加载中。。。。");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                progressDialog.dismiss();
            }
        }, 1000 * 3);
    }

    private void showAlertDialog3() {
        View view = View.inflate(this, R.layout.dialog_layout, null);
        final EditText username = view.findViewById(R.id.username_dialog);
        final EditText password = view.findViewById(R.id.password_dialog);

        new AlertDialog.Builder(this).setTitle("Title")
                //                .setView(R.layout.dialog_layout)
                .setView(view).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this, username.getText().toString() + ":" + password.getText().toString(),
                               0).show();
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                Toast.makeText(DialogActivity.this, "取消", 0).show();
            }
        }).show();
    }

    private void showAlertDialog2() {
        final String[] strings = {"红", "黄", "绿"};
        new AlertDialog.Builder(this).setTitle("Title").setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this, "确定", 0).show();
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this, "取消", 0).show();
            }
        }).setSingleChoiceItems(strings, 1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this, "选中的是" + strings[which], 0).show();
                dialog.dismiss();
            }
        }).show();
    }

    private void showAlertDialog() {
        new AlertDialog.Builder(this).setTitle("Title").setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this, "确定", 0).show();
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DialogActivity.this, "取消", 0).show();
            }
        }).show();
    }
}
